/**
  ******************************************************************************
  * @file    drv_dbuff.c
  * @author  zpw
  * @version V1.0
  * @date    
  * @brief   双缓冲实现
  ******************************************************************************
  * @attention
  *
  * 
  * 
  * 
  *
  ******************************************************************************
  */

#include "drv_dbuff.h"
#include "stdlib.h"
#include "string.h"

void dbuff_init(DBUFF *dbuff, uint8_t *dpA, uint16_t lenA, uint8_t *dpB, uint16_t lenB)
{
#if USE_MALLOC
    dbuff->queA = (QUEUE *)malloc(sizeof(QUEUE));
    dbuff->queB = (QUEUE *)malloc(sizeof(QUEUE));
#else
    //静态
#endif
    queue_init(dbuff->queA, dpA, lenA);
    queue_init(dbuff->queB, dpB, lenB);
    dbuff->which = 0;
}

void dbuff_switch(DBUFF *dbuff)
{

    //如果现在是A，则准备buffB
    if (dbuff->which == 0)
    {
        queue_reset(dbuff->queB);
    }
    else
    {
        queue_reset(dbuff->queA);
    }
    //切换buff
    dbuff->which = 1 - dbuff->which;
}

uint8_t *dbuff_get_data(DBUFF *dbuff)
{

    if (dbuff->which == 0)
    {

        return dbuff->queA->data;
    }
    else
    {

        return dbuff->queB->data;
    }
}

uint8_t dbuff_get_size(DBUFF *dbuff)
{

    if (dbuff->which == 0)
    {

        return queue_size(dbuff->queA);
    }
    else
    {

        return queue_size(dbuff->queB);
    }
}

void dbuff_push(DBUFF *dbuff, uint8_t *data, uint16_t size)
{

    if (dbuff->which == 0)
    {

        queue_push(dbuff->queA, data, size);
    }
    else
    {

        queue_push(dbuff->queB, data, size);
    }
}

void dbuff_pop(DBUFF *dbuff, uint8_t *data, uint16_t size)
{

    if (dbuff->which == 0)
    {

        queue_pop_data(dbuff->queA, data, size);
    }
    else
    {

        queue_pop_data(dbuff->queB, data, size);
    }
}
